using UnityEngine;
using System.Collections;

public class Movement: MonoBehaviour {
	
	
	public int moveSpeed = 180;
	public bool isComputer = false;
	Vector3 computerDirection = Vector3.right;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
		if(!isComputer)PlayerMove();
		else ComputerMove ();
		
	}
	
	void ComputerMove()
	{
		Vector3 newPosition = Vector3.zero;
		newPosition = computerDirection * (moveSpeed * Time.deltaTime);
		
		newPosition = newPosition + transform.position;
		
		if(newPosition.x > 130)
		{
			newPosition.x = 130;
			computerDirection.x *= -1;
		}
		else if(newPosition.x < -130)
		{
			newPosition.x = -130;
			computerDirection.x *= -1;
		}
		transform.position = newPosition;
	}
	
	void PlayerMove()
	{
		Vector3 moveDirection = Vector3.zero;
		Vector3 newPosition = Vector3.zero;
		
		if(Input.GetKey(KeyCode.RightArrow))moveDirection.x += 1;
		if(Input.GetKey(KeyCode.LeftArrow))moveDirection.x -= 1;
		
		moveDirection = moveDirection * (moveSpeed * Time.deltaTime);
		
		transform.position = transform.position + moveDirection;
		
		newPosition = transform.position + moveDirection;
		
		newPosition.x = Mathf.Clamp(newPosition.x, -130, 130);
		
		transform.position = newPosition;
	}
	
	
}
